<?php

class OrderProduct extends Eloquent {

	 protected $table = 'order_product';

	/**
	 * Condition: Unarchived Items.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @param $query
	 * @return mixed
	 */
	public function scopeActive( $query ) {
		return $query->where($this->table . '.archived', 0);
	}

	/**
	 * Condition: Unarchived Parent Order.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @param $query
	 * @return mixed
	 */
	public function scopeActiveOrder( $query ) {
		return $query->whereHas('order', function( $q ){
			$q->where('archived', 0);
		});
	}

	/**
	 * Relates to Manufacturer.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function manufacturer() {
		return $this->belongsTo('Manufacturer', 'MANUFACTURER_id');
	}

	/**
	 * Relates to Size.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function size() {
		return $this->belongsTo('Size', 'SIZE_id');
	}

	/**
	 * Relates to Category.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function category() {
		return $this->belongsTo('Category', 'CATEGORY_id');
	}

	/**
	 * Relates to Product.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function product() {
		return $this->belongsTo('Product', 'PRODUCT_id');
	}

	/**
	 * Relates to Order.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function order() {
		return $this->belongsTo('Order', 'ORDER_id');
	}

	/**
	 * Retrieves Product Options of Ordered Item.
	 * Usage: OrderProduct::first()->orderProductOptions->option_value;
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function orderProductOptions() {
		return $this->hasMany('OrderProductOption', 'ORDER_id', 'ORDER_id')
			->where('PRODUCT_id', $this->PRODUCT_id);
	}
	
	/**
	 * Retrieves Order Set of Ordered Item.
	 * Usage: OrderProduct::first()->orderSet->zone_fee;
	 * @author AngelaMisa 03/09/2015 4:53PM
	 * @return mixed
	 */
	public function orderSet() {
		return $this->hasOne('OrderSet', 'ORDER_id', 'ORDER_id')
			->where('SET_id', $this->SIZE_id);
	}
}